package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.entity.Result;
import com.qf.entity.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author TZL
 * @date 2020/7/19
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //增 insertUser
    @ResponseBody
    @PostMapping("/insertUser")
    public Result insertUser(@RequestBody User user){
        userService.insertUser(user);
        return Result.ok();
    }

    //删 deleteUser
    @DeleteMapping("/deleteUser/{id}")
    @ResponseBody
    public Result deleteUser(@PathVariable int id){
        userService.deleteUser(id);
        return Result.ok();
    }

    //修改
    @PutMapping("/upusers")
    @ResponseBody
    public Result updateUser(@RequestBody  User user){
        userService.updateUser(user);
        return Result.ok();
    }

    //查所有 queryUserAll
    @GetMapping("/users")
    public String queryUserAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "2") int pageSize, Model model){//model向页面传参用
        PageInfo<User> userPageInfo = userService.queryUserAll(pageNum,pageSize);
        model.addAttribute("pageInfo",userPageInfo);
        return "users";
    }

    //查单个 selectUserById
    @GetMapping("/user/{id}")
    public String selectUserById(@PathVariable int id,Model model){
        User user = userService.selectUserById(id);
        model.addAttribute("user",user);
        return "user";
    }

}
